/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.Image;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.list.DefaultListModel;
import core.ScreenEngine;
import datamodel.Message;
import view.additional.MessageListRenderer;
import view.commands.BackCommand;

/**
 *
 * @author Kvest
 */
public class MessagessListForm extends BasicForm implements ActionListener
{
    private static final int LOOK_COMMAND_ID = 1;
    
    private final List items = new List();
    
    private String titleString;
    
    public MessagessListForm(Image readIcon, Image unreadIcon)
    {
        super("messages");
        
        titleString = getTitle();
        
        init(readIcon, unreadIcon);
    }
    
    private void init(Image readIcon, Image unreadIcon)
    {
        setLayout(new BorderLayout());
        setScrollable(false);
        
        items.setSelectCommandText("");
        items.setRenderer(new MessageListRenderer(readIcon, unreadIcon));
        items.setFixedSelection(List.FIXED_NONE_CYCLIC);
        items.addActionListener(this);
        addComponent(BorderLayout.CENTER, items);
        
        //Команды на форме
        addCommand(new BackCommand());
        
        Command look = new Command("look", LOOK_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                Message selected;
                synchronized(items)
                {
                    selected = (Message)items.getSelectedItem();
                }
                itemSelected(selected);
        
                evt.consume();
            }
        
        };
        addCommand(look);
    }
    
    public void actionPerformed(ActionEvent evt)
    {
        Message selected;
        synchronized(items)
        {
            selected = (Message)items.getSelectedItem();
        }
        itemSelected(selected);
        
        evt.consume();
    }
    
    private void itemSelected(Message selected)
    {
        if (selected == null)
        {
            return;
        }
        
        //Отображаем сообщение
        ScreenEngine.getEngine().showMessage("message", selected, 0);
    }
    
    public void setCountToTitle(int unreadCount, int totalCount)
    {
        setTitle("(" + unreadCount + "/" + totalCount + ") " +  titleString);
    }
    
    public void clear()
    {
        synchronized(items)
        {
            ((DefaultListModel)items.getModel()).removeAll();
            items.setSelectedIndex(0);
        }
    }
    
    public void addMessage(Message message)
    {
        synchronized(items)
        {
            items.getModel().addItem(message);
        }
        
        //Перерисовываем
        if (isVisible())
        {
            repaint();
        }
    }
    
    public void addMessage(Message message, int index)
    {
        synchronized(items)
        {
            ((DefaultListModel)items.getModel()).addItemAtIndex(message, index);
        }
        
        //Перерисовываем
        if (isVisible())
        {
            repaint();
        }
    }
}
